package com.bj58.disconf.web.controllers;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bj58.disconf.web.common.constant.WebConstants;
import com.bj58.disconf.web.controllers.app.validator.AppValidator;
import com.bj58.disconf.web.service.app.form.AppNewForm;
import com.bj58.disconf.web.service.app.service.AppMgr;
import com.bj58.disconf.web.service.app.vo.AppListVo;
import com.bj58.disconf.web.utils.GuiceDI;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.annotation.Path;

/**
 * @author lemon
 * @version 2014-6-16
 */
@Controller
@RequestMapping(WebConstants.API_PREFIX + "/app")
@Path("/api/app")
public class AppController extends BaseController {

    protected static final Logger LOG = LoggerFactory.getLogger(AppController.class);

    /**
     * list
     * 
     * @return
     */
    @Path("/list")
    public ActionResult list() {
        try {
            List<AppListVo> appListVos = GuiceDI.getInstance(AppMgr.class).getAuthAppVoList();

            return new JsonActionResult(buildListSuccess(appListVos, appListVos == null ? 0 : appListVos.size()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * create
     *
     * @return
     */
    @Path("")
    public ActionResult create(@Valid AppNewForm appNewForm) {

        try {
            LOG.info(appNewForm.toString());

            GuiceDI.getInstance(AppValidator.class).validateCreate(appNewForm);

            GuiceDI.getInstance(AppMgr.class).create(appNewForm);

            return new JsonActionResult(buildSuccess("创建成功"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
